package com.oddfar.campus.business.controller;

import com.oddfar.campus.business.domain.Czxx;
import com.oddfar.campus.business.mapper.CzxxMapper;
import com.oddfar.campus.business.service.CzxxService;
import com.oddfar.campus.common.annotation.Anonymous;
import com.oddfar.campus.common.domain.PageResult;
import com.oddfar.campus.common.domain.R;
import com.oddfar.campus.common.domain.entity.SysRoleEntity;
import com.oddfar.campus.common.domain.entity.SysUserEntity;
import com.oddfar.campus.common.exception.ServiceException;
import com.oddfar.campus.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/fjdcgl/czxx")
public class CzxxController {

    @Autowired
    private CzxxService czxxService;

    @Autowired
    private CzxxMapper czxxMapper;

    @RequestMapping(value = "/list", name = "查询车主列表")
    @PreAuthorize("@ss.hasAnyRoles('common,admin,user')")
    public R list(Czxx czxx) {
        PageResult<Czxx> page = czxxService.page(czxx);
        return R.ok().put(page);
    }

    /**
     * 获取用户详细信息
     */
    @PreAuthorize("@ss.hasAnyRoles('common,admin,user')")
    @GetMapping(value = "/{mobile}", name = "获取车主详细信息")
    public R getInfo(@PathVariable("mobile") Long mobile) {
        return R.ok(czxxMapper.selectById(mobile));
    }

    /**
     * 新增车主信息
     */
    @PreAuthorize("@ss.hasAnyRoles('common,admin,user')")
    @PostMapping(name = "新增车主信息")
    public R add(@RequestBody Czxx czxx) {
        return R.ok(czxxService.insertCzxx(czxx));
    }

    /**
     * 修改车主信息
     */
    @PreAuthorize("@ss.hasAnyRoles('common,admin,user')")
    @PutMapping(name = "修改车主信息")
    public R edit(@RequestBody Czxx czxx) {
        return R.ok(czxxService.updateCzxx(czxx));
    }

    /**
     * 修改密码
     */
    @PreAuthorize("@ss.hasAnyRoles('common,admin,user')")
    @PutMapping(value = "/resetPwd", name = "修改密码")
    public R resetPwd(@RequestBody Czxx czxx) {
        return R.ok(czxxService.updateCzxx(czxx));
    }

    /**
     * 删除车主信息
     */
    @PreAuthorize("@ss.hasAnyRoles('common,admin,user')")
    @DeleteMapping(value = "/{userid}", name = "删除车主信息")
    public R remove(@PathVariable Long userid) {
        return R.ok(czxxMapper.deleteCzxx(userid));
    }

}
